import time
import math

class Actuator(object):
    def __init__(self,start, end,duration):
        self.restart(start,end,duration)
    
    def restart(self,start,end,duration):
        self.X=start
        self.Y=end
        self.startT=time.clock()
        self.endTime=self.startT+duration
        self.duration=duration
        
    def val(self):
        t=time.clock()
        if t>=self.endTime:
            return (True,self.Y)
        
        return (False,self.func(self.X,self.Y))
    
    def func(self,a,b):
        t=time.clock()
        t=(t-self.startT)/self.duration
        
        return a+(b-a)*t
  

  
    
class SineActuator(Actuator):
    
    def __init__(self,start, end,duration):
        super(SineActuator,self).__init__(start, end,duration)
        
    
    
    def func(self,a,b):
        t=time.clock()
        t=(t-self.startT)/self.duration
        angle=3.141592*0.5*(2*t-1)
        return (1+math.sin(angle))*0.5*(b-a)+a
    
class SqrActuator(Actuator):
    
    def __init__(self,start, end,duration):
        super(SqrActuator,self).__init__(start, end,duration)
        
    
    
    def func(self,a,b):
        t=time.clock()
        t=(t-self.startT)/self.duration
        t=t**3
        return a+(b-a)*t

class InvSqrActuator(Actuator):
    
    def __init__(self,start, end,duration):
        super(InvSqrActuator,self).__init__(start, end,duration)
        
    
    
    def func(self,a,b):
        t=time.clock()
        t=(t-self.startT)/self.duration
        t=(1.-t)**2
        return a+(b-a)*(1.-t)
